﻿// Copyright (C) 2012 Kim Jensen, TaskJunction.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskJunctionLib.Mapping.JSON;
using TaskJunctionLib.Model;

namespace TaskJunctionLib.Repository
{
    public class IterationRepository : CachableRepository<Iteration, IterationMapper>, IIterationRepository
    {
        protected override string GetResourceName()
        {
            return "iterations";
        }

        public IList<Iteration> LoadByProjectId(int projectId)
        {
            return GetList(projectId);
        }

        private IList<Iteration> GetList(int projectId)
        {
            if (!UseCaching || Cache == null || projectId != CacheProjectId)
            {
                ClearCache();
                var list = _LoadList(GetResourceNameByValues(new Dictionary<string, string> { { "project_id", projectId.ToString() } }));
                CacheProjectId = projectId;
                return list;
            }
            return Cache;
        }

        public override void ClearCache()
        {
            CacheProjectId = 0;
            Cache = null;
        }

        private int CacheProjectId;
    }
}
